/*
* @Author: dengjiayao
* @Date:   2017-09-08 09:38:50
 * @Last Modified by: songwh
 * @Last Modified time: 2018-02-03 16:21:26
*/
import Pages from '../../component/pages/pages.vue'
import Config from '../customer-config/customer-config.vue'
import { showMsg, getNewShareCustomers, updateStatus } from '../../vuex/action'

export default {
  name: 'customer-list',
  components: {
    'vue-pages': Pages,
    'vue-config': Config
  },
  data() {
    return {
      list: [],
      pages: {
        start: 0,
        totalPages: 0,
        limit: 20
      },
      filter: {
        stockCode: '',
        username: '',
        userNo: '',
        status: ''
      }
    }
  },
  vuex: {
    getters: {
      fresh: state => state.hkListNeedFresh
    },
    actions: {
      xShowMsg: showMsg,
      xGetList: getNewShareCustomers,
      xUpdate: updateStatus
    }
  },
  created() {
    this.getList({})
  },
  methods: {
    getList({start=this.pages.start, limit=this.pages.limit}) {
      let params = {
        start: start,
        limit: limit
      }
      let {stockCode, username, userNo, status} = this.filter
      if (stockCode.length) {
        params.stockCode = stockCode
      }
      if (username.length) {
        params.username = username
      }
      if (userNo.length) {
        params.userNo = userNo
      }
      if (status !== '') {
        params.status = status
      }

      this.xGetList(params)
        .then((data) => {
          this.list = data.list
          this.pages.totalPages = Math.ceil(data.count / limit)
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    },
    search() {
      this.pages.start = 0
      this.getList({})
    },
    updateItemStatus(id, newStatus) {
      this.xUpdate({
        id: id,
        status: newStatus + ''
      })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    },
    modify({
      id = '',
      phone = '',
      userNo = '',
      stockCode = '',
      num = 0,
      price = '',
      finalNum = '',
      totalSpend = '',
      remark = '',
      status = 0,
      serviceCharge = '50.000',
      uid = '',
      userName = ''
    }) {
      let ref = this.$refs.config
      ref.actionType = 'UPDATE'
      ref.form = {
        id: id,
        phone: phone,
        userNo: userNo,
        stockCode: stockCode,
        num: num,
        price: price,
        finalNum: finalNum,
        totalSpend: totalSpend,
        remark: remark,
        status: status,
        serviceCharge: serviceCharge,
        uid: uid,
        userName: userName
      }
      ref.showModal = true
    },
    addItem() {
      let ref = this.$refs.config
      ref.actionType = 'ADD'
      ref.form = {
        phone: '',
        userNo: '',
        stockCode: '',
        num: '',
        price: '',
        finalNum: '',
        totalSpend: '',
        remark: '',
        status: 1,
        serviceCharge: '50.000',
        uid: '',
        userName: ''
      }
      ref.showModal = true
    }
  },
  watch: {
    fresh(val) {
      if (val) {
        this.getList({})
      }
    }
  }
}
